package itsm.isperp.framework.security.encoding;

import itsm.isperp.framework.security.access.vote.DefaultAuthenticatedVoter;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.encoding.PasswordEncoder;

/**
 * 密码解析器
 * 
 * @author lizx
 * @date 2012-11-7
 * @version V1.0
 */
public class DESPasswordEncoder implements PasswordEncoder {

	private Log logger = LogFactory.getLog(DESPasswordEncoder.class);

	@Override
	public String encodePassword(String rawPass, Object salt) {
		try {
			return DESHelper.encrypt(rawPass, salt.toString());
		} catch (Exception ex) {
			return null;
		}
	}

	@Override
	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		try {
			// String encPass2 = DESHelper.encrypt(rawPass, salt.toString());

			String encPass2 = DESHelper.encrypt(rawPass);

			logger.debug("login rawPass:" + rawPass + ";salt:" + salt);
			logger.debug("login encPass:" + encPass + ";encPass2:" + encPass2);

			String encPass3 = new String(
					Hex.encodeHex(DigestUtils.md5(rawPass)));

			if (encPass != null
					&& (encPass.equals(encPass2) || encPass.equals(rawPass))
					|| encPass.equals(encPass3)) {
				return true;
			}
		} catch (Exception ex) {
			return false;
		}
		return false;
	}

}
